const app = getApp()
var webUrl = app.webUrl;
var localUrl = app.localUrl;
Page({
    data:{
        show:false,
        start:0,
        isEmpty:true
    },
    onLoad(){
        console.log(app.global);
        var self = this;
        wx.request({
            url: webUrl+"top250",
            header: {
                'Content-Type': 'json'
            },
            success: function(res) {
                self.handleData(res);
            }
        });
    },
    handleData(res){
        var totalData= [];
        var data =[];
        var subject = res.data.subjects;
        for(let key in subject){
            var name = subject[key].title;
            var average = subject[key].rating.average;
            var imgUrl = subject[key].images.small;
            var id = subject[key].id;
            var temp = {};
            if(name.length>8){
                name = name.slice(0,7);
            }
            temp.name=name;
            temp.average=average;
            temp.imgUrl=imgUrl;
            temp.id = id;
            data.push(temp);
        }
        if (!this.data.isEmpty){
            totalData = this.data.data.concat(data);
            console.log(totalData);
            this.setData({
            data:totalData,
            show:true
        });
    }else{
        this.setData({
            data:data,
            show:true
        });
        this.data.isEmpty = false;
      }
      wx.hideNavigationBarLoading();
    },
    handleClick(event){
        var id = event.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/movie-detail/movie-detail?id='+id
        });
    },
    onReachBottom:function(){
        wx.showNavigationBarLoading();
        var self = this;
        this.data.start+=20;
        var url= "https://douban.uieee.com/v2/movie/top250";
        var nextUrl = url+"?start="+this.data.start+"&count=20";
        wx.request({
            url: nextUrl,
            header: {
                'Content-Type': 'json'
            },
            success: function(res) {
                self.handleData(res);
            }
        });
    }
});